/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bcamone.bluetooth;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.*; //jsr082.jar
import java.io.IOException;
import java.util.Vector;

/**
 * This class handels the bluetoothconnection
 * @author Filktiv
 * @version 0.1
 */

public class BlueConnectionManager {
    
    // discovery Object
    private RemoteDeviceDiscovery blueDisc;
    // Count of discoveries
    private int discCount = 0;
    
    /**
     * Constructor has nothing to do at the moment
     * no params
     */
    
    public BlueConnectionManager ()
    {
        // still nothing to do
    }
    
    /**
     * Checks if local bluetooth device is on
     * @return boolean, true is device is on
     */
    
    public boolean blueIsOn()
    {
        return LocalDevice.isPowerOn();
    }
    
    /**
     * delivers the local bluetooth device
     * @return String, local device
     */
    
    public String getBlueDeviceName() throws BluetoothStateException
    {
        String returnText;
        try{
        returnText = LocalDevice.getLocalDevice().toString();
        }
        catch (BluetoothStateException e){
            returnText = "NO STACK FOUND";
        }
        return returnText;
    }
    
    /**
     * Delivers the founded remote devices.
     * If no search was already made, it first searches for devices.
     * @return Vector, foundet remote devices
     */
    
    public Vector getDeviceList()
    {
        if (discCount <= 0)
        {
            try {
                discovery();
            } catch (IOException ex) {
                Logger.getLogger(BlueConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return blueDisc.getDeviceList();
    }
    
    /**
     * Does the search for remote devices
     */
    
    private void discovery() throws IOException
    {
        discCount++;
        blueDisc = new RemoteDeviceDiscovery();
    }
    
}
